//
//  PersonalViewController.m
//  firstpoint
//
//  Created by 王旭阳 on 2017/6/23.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "PersonalViewController.h"
#import "GerRenXinXiViewController.h"
#import "ShoppingCartViewController.h"
#import "WoDeTableViewCell.h"
#import "MyOrderViewController.h"
#import "LeeMyFavoritesViewController.h"//我的收藏
#import "MyAccountViewController.h"
#import "AboutAppViewController.h"//关于原点
#import "HelpDescriptionViewController.h"//帮助说明
#import "FeedbackViewController.h"
#import "LeeShippingAddressViewController.h"//收货地址
#import "NameAuthenticationViewController.h"
#import "SystemMessageViewController.h"//系统消息
#import "RecommendViewController.h"
#import "LeeMyShouCangRenWuViewController.h"
#import "ShenHeTongGuoViewController.h"//审核通过
#import "ShenHeShiBaiViewController.h"//审核失败
#import "ShenHeZhongViewController.h"//审核中
#import "LoginViewController.h"//登录
@interface PersonalViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView*tabView;//表
    UIButton*headView;//区头
    UIImageView*touxiangImg;//头像按钮
    UILabel*nameLab;//姓名lab
    UILabel*dengjiLab;//等级Lab
    UILabel*chengzhangLab;//成长值
    NSArray*imgArr;
    NSArray*titleArr;
    NSDictionary*dataDic;
    UIActivityIndicatorView *juhuaLod;
    NSDictionary*userInfoDic;
}
@end
@implementation PersonalViewController
-(void)myInfoHttpRequest
{  NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"/user/accountInfo.api?access_token=%@",tokenStr];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        
        userInfoDic= [PublicTools dictionaryWithJsonString:responseObject[@"data"][@"userDTO"]];
     
        NSURL*url=[NSURL URLWithString:LINK_BASE_URL(userInfoDic[@"headUrl"])];
        [touxiangImg setImageWithURL:url];
        nameLab.text=userInfoDic[@"realName"];
        dengjiLab.text=[NSString stringWithFormat:@"等级:%@",userInfoDic[@"level"]];

    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self myInfoHttpRequest];
    self.navigationItem.leftBarButtonItem = nil;

    titleArr=@[@[@"我的账户",@"实名认证"],@[@"消息中心",@"向好友推荐"],@[@"我的收藏-任务",@"我的收藏-商品",@"我的订单",@"购物车",@"收货地址"],@[@"意见反馈",@"帮助说明",@"关于元点"]];
    UIImage*img1=[UIImage imageNamed:@"grzxwdzh"];
    UIImage*img2=[UIImage imageNamed:@"shimingrenzheng"];
    UIImage*img3=[UIImage imageNamed:@"grzxxtxx"];
    UIImage*img4=[UIImage imageNamed:@"grzxxhytj"];
    UIImage*img6=[UIImage imageNamed:@"grzxwdscrw"];
    UIImage*img7=[UIImage imageNamed:@"grzxwdscssp"];
    UIImage*img8=[UIImage imageNamed:@"grzxwddd"];
    UIImage*img9=[UIImage imageNamed:@"grzxgwc"];
    UIImage*img10=[UIImage imageNamed:@"shouhuodizhi"];
    UIImage*img11=[UIImage imageNamed:@"xxgrzxyjfk-1"];
    UIImage*img12=[UIImage imageNamed:@"xxgrzxbzsm"];
    UIImage*img13=[UIImage imageNamed:@"shdgyyd"];

    
    imgArr=@[@[img1,img2],@[img3,img4],@[img6,img7,img8,img9,img10],@[img11,img12,img13]];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"个人中心";
    [self setTab];
    [self setqutou];
}
-(void)setqutou//区头
{
    headView=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width_screen, 77)];
    headView.backgroundColor=[UIColor whiteColor];
    [headView addTarget:self action:@selector(gerenxinxiClick) forControlEvents:UIControlEventTouchUpInside];
    touxiangImg=[UIImageView new];
    touxiangImg.frame=CGRectMake(20, 13.5, 50, 50);
    touxiangImg.layer.cornerRadius=25;
    touxiangImg.backgroundColor=[UIColor redColor];
    touxiangImg.layer.masksToBounds = YES;

    [headView addSubview:touxiangImg];
    nameLab=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, width_screen-80, 20)];
    nameLab.text=@"王旭阳";
    [headView addSubview:nameLab];
    dengjiLab=[[UILabel alloc]initWithFrame:CGRectMake(80, 45, 100, 15)];
    dengjiLab.font=[UIFont systemFontOfSize:15];
    dengjiLab.textColor=[UIColor grayColor];
    dengjiLab.text=@"等  级   100";
    [headView addSubview:dengjiLab];
    tabView.tableHeaderView=headView;
}

-(void)gerenxinxiClick
{
    GerRenXinXiViewController*vc=[GerRenXinXiViewController new];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setTab
{
    tabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, width_screen, height_screen-64-50) style:1];
    tabView.separatorStyle=0;
    tabView.delegate=self;
    tabView.dataSource=self;
    [tabView registerClass:[WoDeTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:tabView];
    [self setFootView];
    
}
-(void)setFootView
{
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 65)];
    tabView.tableFooterView=view;

    UIButton*footBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    footBtn.frame=CGRectMake(10, 10, width_screen-20, 45);
    footBtn.backgroundColor=LRRGBColor(215, 25, 36);
    [footBtn setTitle:@"退出登录" forState:0];
    [footBtn addTarget:self action:@selector(escButtonClick) forControlEvents:64];
    footBtn.layer.cornerRadius=5;
    [view addSubview:footBtn];
};
-(void)escButtonClick
{
    [NSUserDefaults resetStandardUserDefaults];
    LoginViewController *vc = [LoginViewController new];
    self.view.window.rootViewController=vc;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 2;
    }
    if (section==1) {
        return 2;
    }
    if (section==2) {
        return 5;
    }
    if (section==3) {
        return 3;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WoDeTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.imgView.image=imgArr[indexPath.section][indexPath.row];
    cell.lab.text=titleArr[indexPath.section][indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"sss");
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            MyAccountViewController*vc=[MyAccountViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            juhuaLod= [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            juhuaLod.center = CGPointMake(100.0f, 100.0f);
            juhuaLod.frame=CGRectMake(width_screen-100, 0, 100, 49);
            juhuaLod.color = [UIColor redColor];
            juhuaLod.backgroundColor=[UIColor whiteColor];
            
            [juhuaLod startAnimating];
            UITableViewCell*cell = [tableView cellForRowAtIndexPath:indexPath];
            [cell addSubview:juhuaLod];
            NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"/member/getMemberData.api?access_token="),NSUD_Obj(@"token")];
            [HTTPURL postRequest:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                NSLog(@"%@",responseObject);
                dataDic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
                NSLog(@"%@",dataDic);
                [juhuaLod removeFromSuperview];

                //未操作
//                if ([dataDic[@"status"] isEqualToString:@""]) {
//                    NameAuthenticationViewController*vc=[NameAuthenticationViewController new];
//                    vc.hidesBottomBarWhenPushed=YES;
//                    [self.navigationController pushViewController:vc animated:YES];
//
//                }
//                //审核通过
                if ([dataDic[@"status"] isEqualToString:@"1"]) {
                    ShenHeTongGuoViewController*vc=[ShenHeTongGuoViewController new];
                    vc.hidesBottomBarWhenPushed=YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                //审核失败
//                if ([dataDic[@"status"] isEqualToString:@"2"]) {
                    ShenHeShiBaiViewController*vc=[ShenHeShiBaiViewController new];
                    vc.hidesBottomBarWhenPushed=YES;
                    [self.navigationController pushViewController:vc animated:YES];
//                }
               //审核中
                if ([dataDic[@"status"] isEqualToString:@"3"]) {
                    ShenHeZhongViewController*vc=[ShenHeZhongViewController new];
                    vc.hidesBottomBarWhenPushed=YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            } filure:^(NSURLSessionDataTask *task, id error) {
                NSLog(@"%@",error);
                [juhuaLod removeFromSuperview];

            }];

            
        }
        
    }
    if (indexPath.section==1) {
        if (indexPath.row==0) {
            SystemMessageViewController*vc=[SystemMessageViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
        
            RecommendViewController*vc=[RecommendViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
   
    if (indexPath.section==2) {
        if(indexPath.row==0){
            LeeMyShouCangRenWuViewController *vc = [LeeMyShouCangRenWuViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==1) {
            LeeMyFavoritesViewController*vc=[LeeMyFavoritesViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }        if (indexPath.row==2) {
            MyOrderViewController*vc=[MyOrderViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];

        }
        if (indexPath.row==3) {
            ShoppingCartViewController*vc=[ShoppingCartViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==4) {
            LeeShippingAddressViewController*vc=[LeeShippingAddressViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (indexPath.section==3) {
        
        if (indexPath.row==0) {
            FeedbackViewController *vc=[FeedbackViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==1) {
            
            HelpDescriptionViewController *vc=[HelpDescriptionViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        if (indexPath.row==2){
            
           AboutAppViewController *vc=[AboutAppViewController new];
            vc.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
